package tool;
import book.Book;
import user.User;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Tools {
    public static ArrayList<Book> readToArray(String filename)
    {
        ArrayList<Book> books = new ArrayList<>();
        try (FileReader reader = new FileReader(filename);
             BufferedReader br = new BufferedReader(reader))
        {
            String line ;
            while((line = br.readLine() )!=null)
            {
                String[] s = line.split(",");
                int id = Integer.parseInt(s[0]);
                String name = s[1];
                String author = s[2];
                double price = Double.parseDouble(s[3]);
                String type = s[4];
                int love = Integer.parseInt(s[5]);
                int state = Integer.parseInt(s[6]);
                boolean isBorrowed = Boolean.parseBoolean(s[7]);
                books.add(new Book(id,name,author,price,type,love,state,isBorrowed));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return books;
    }

    public static void writeToFile(ArrayList<Book> books,String filename)
    {
        try( FileWriter writer = new FileWriter(filename);
        BufferedWriter bw = new BufferedWriter(writer))
        {
           for(Book book:books)
           {
               bw.write(book.toStringToFile());
               bw.newLine();
           }
           bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //同步历史借阅的书与书目的书籍信息一致，并重新写入历史借阅文件
    public static void synchronousHBFromBL(ArrayList<Book> books,ArrayList<Book> HistoryBorrowingBooks)
    {
        //通过遍历历史借阅书籍的信息和书目的书籍信息进行对比，将借阅的书籍信息更新，与书目信息一致
        // 从而使查看我的借阅记录使书籍的信息(如点赞量等)与书目一致
        for(int i=0;i<HistoryBorrowingBooks.size();i++)
        {
            Book book1 =HistoryBorrowingBooks.get(i);
            for(int j=0;j<books.size();j++)
            {
                Book book2 =books.get(j);
                if(book1.getName().equals(book2.getName()))
                {
                    book1.setState(book2.getState());
                    book1.setLove(book2.getLove());
                    book1.setBorrowed(book2.isBorrowed());
                    break;
                }
            }
        }
        //更新完后随及写进文件，实现实时操作
        //进行写入操作
        writeToFile(HistoryBorrowingBooks,"File\\HistoryBorrowing.txt");
    }

    //---------------------积分操作----------------
    //将排行榜的信息读到数组中
    private static void readRankingFileToArray(ArrayList<String> PointArray){
        try (Reader reader = new FileReader(new File("File\\ranking.txt"));
             BufferedReader bufferedReader = new BufferedReader(reader))
        {
            while(true)
            {
                String line = bufferedReader.readLine();
                if(line==null)break;
                PointArray.add(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //对排行榜的信息进行排行
    private static void sortPoint(User user,ArrayList<String> PointArray){
        readRankingFileToArray(PointArray);
        //先将用户的原始积分和当前积分进行累加
        for(int i =0;i<PointArray.size();i++)
        {
            String[] s1 = PointArray.get(i).split(":");
            String userName = s1[0];
            int point=Integer.parseInt(s1[1]);
            if(userName.equals(user.getName()))
            {
                point+= user.getPoint();
                String newPoint = String.valueOf(point);
                String tem = userName+":"+newPoint;
                PointArray.set(i,tem);
                break;
            }
        }

        //降序排序
        for(int i =0;i<PointArray.size()-1;i++)
        {
            for(int j =0;j<PointArray.size()-i-1;j++)
            {
                String[] s1 = PointArray.get(j).split(":");
                String[] s2 = PointArray.get(j+1).split(":");
                String userName = s1[0];
                int point1=Integer.parseInt(s1[1]);
                int point2=Integer.parseInt(s2[1]);
                if(point1<point2)
                {
                    Collections.swap(PointArray,j,j+1);
                }
            }
        }

    }

    //判断目前的用户是否在积分文件中，不在则添加进积分数组
    private static void uersIsInRankingFile(User user,ArrayList<String> PointArray)
    {
        try (Reader reader = new FileReader(new File("File\\ranking.txt"));
             BufferedReader bufferedReader = new BufferedReader(reader))
        {
            if(user.getName().equals("admin")) return;//管理员不需要写入排行文件
            while(true)
            {
                String line = bufferedReader.readLine();
                if(line==null)break;
                String[] s =line.split(":");
                if(s[0].equals(user.getName()))
                {
                    return;
                }
            }
            String p =String.valueOf(user.getPoint());
            PointArray.add(user.getName()+":"+p);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //将积分写到文件里
    public static void savePointsToFile(User user,ArrayList<String> PointArray)
    {
        uersIsInRankingFile(user,PointArray);
        sortPoint(user,PointArray);
        try ( Writer writer = new FileWriter(new File("File\\ranking.txt"));
              BufferedWriter bw = new BufferedWriter(writer) )
        {
            for(int i=0;i<PointArray.size();i++)
            {
                bw.write(PointArray.get(i));
                bw.newLine();
            }
            bw.flush();
            PointArray.clear();//写完后清空数组
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //---------------------积分操作----------------


}
